<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
const isRegister = ref(false)
const changeLogin = () => {
  isRegister.value = !isRegister.value
}
const form = ref()
// 用于提交的from数据对象
const formModel = ref({
  username: '',
  password: '',
  repassword: ''
})
// 监测表单切换的时候，重置表单
watch(isRegister, () => {
  formModel.value = {
    username: '',
    password: '',
    repassword: ''
  }
})

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    {
      min: 5,
      max: 10,
      message: '用户名必须是5-10位的字符',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15位的非空字符',
      trigger: 'blur'
    }
  ],
  repassword: [
    // 首先要和原来的密码校验一致
    { required: true, message: '请输入密码', trigger: 'blur' },
    {
      pattern: /^\S{6,15}$/,
      message: '密码必须是6-15的非空字符',
      trigger: 'blur'
    },
    {
      // 自定义的校验规则
      validator: (rule, value, callback) => {
        // 判断value和手机的表单中的数据是都一致
        if (value !== formModel.value.password) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback() //就算校验成功也需要callback
        }
      },
      trigger: 'blur' //这是默认的
    }
  ]
}
const register = async () => {
  // await会在校验成功后返回并执行下面的打印语句
  // 但是如果校验失败validator会自己数据错误信息
  await form.value.validate()
  ElMessage.success('注册成功')
  isRegister.value = false
}
const router = useRouter()
const login = async () => {
  // 记得要绑定表单ref="form"
  await form.value.validate()

  // 登录成功将token存到本地

  ElMessage.success('登录成功')
  //   跳转到首页
  router.push('/')
}
</script>
<template>
  <el-row class="login-page">
    <el-col :span="12" class="text-container">
      <div class="text">
        Log in to<br />
        your account <br />
        and <br />
        start your<br />
        fashion journey
      </div>
    </el-col>
    <el-col :span="6" :offset="2" class="form">
      <!-- 注册的表单 -->
      <el-form
        size="large"
        v-if="isRegister"
        :model="formModel"
        :rules="rules"
        ref="form"
        autocomplete="off"
      >
        <el-form-item class="title">
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formModel.repassword"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="button"
            :dark="isDark"
            color="#a9985e"
            auto-insert-space
            @click="register"
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="changeLogin">
            ← 登录
          </el-link>
        </el-form-item>
      </el-form>
      <!-- 登录表单 -->
      <el-form
        :model="formModel"
        :rules="rules"
        ref="form"
        autocomplete="off"
        size="large"
        v-else
      >
        <el-form-item class="title">
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input
            v-model="formModel.username"
            :prefix-icon="User"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="flex">
          <div class="flex">
            <el-checkbox name="loginCheckBox">记住我</el-checkbox>
            <el-link type="primary" :underline="false">忘记密码？</el-link>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            class="button"
            :dark="isDark"
            color="#a9985e"
            auto-insert-space
            @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="changeLogin">
            注册 →
          </el-link>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<style lang="scss" scope>
.login-page {
  // 100% 的视口高度
  height: 100vh;
  background: url('@/assets/loginbackground.jpg') no-repeat center center;
  .text-container {
    display: flex; /* 使用 Flexbox */
    justify-content: flex-end; /* 文字靠右 */
    align-items: center; /* 垂直居中 */
    height: 100vh; /* 满高 */
    overflow: hidden; /* 防止溢出 */
    /* 文字样式 */
    .text {
      font-style: italic; /* 意式斜体 */
      font-size: 72px; /* 字体大小 */
      opacity: 0; /* 初始透明度 */
      transform: translateX(-100%); /* 初始位置在左侧外部 */
      animation: slide-in 1s forwards; /* 加载动画 */
      margin-right: 20px;
    }
  }
  .form {
    display: flex;
    flex-direction: column;
    justify-content: center; /* 水平居中 */
    user-select: none;
    opacity: 0; /* 初始透明度 */
    transform: translateX(100%); /* 初始位置在右侧外部 */
    animation: slide-in 1s forwards; /* 加载动画 */

    .title {
      margin: 0 auto;
    }

    .button {
      width: 100%;
    }

    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
    @keyframes slide-in {
      to {
        opacity: 1;
        transform: translateX(0); /* 初始位置 */
      }
    }
  }
}
</style>
